﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using BLL;
using MOD;

namespace MapServer.Adhs
{
    public partial class ads1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sReturnValue = String.Empty;
            if (Request["userId"] != null && Request["sendadid"] != null)
            {
                sReturnValue = SendAds(Convert.ToInt64(Request["userId"]), Convert.ToInt64(Request["sendadid"]));
            }
            //if (context.Request["userId"] != null && context.Request["deleteadid"] != null)
            //{
            //    sReturnValue = DeleteAds(Convert.ToInt32(context.Request["userId"]), Convert.ToInt32(context.Request["deleteadid"]));
            //}

            if (Request["userId"] != null && Request["adIds"] != null)
            {
                sReturnValue = RepeatAds(Convert.ToInt64(Request["userId"]), Request["adIds"].ToString().Trim());
            }
            Response.Write(sReturnValue);
        }
        private String SendAds(Int64 iUserId, Int64 iAdid)
        {

            String sValue = "998";
            try
            {
                B_AdInfo bAo = new B_AdInfo();
                M_Adinfo mAo = new M_Adinfo();
                DataTable dDt = new DataTable();
                int iValue = 0;
                dDt = bAo.SelectUserIdPassWord(iUserId, iAdid);
                if (dDt.Rows.Count == 0)
                {
                    mAo.CreateTime = System.DateTime.Now;
                    mAo.UserId = iUserId;
                    mAo.WeiboId = iAdid;
                    if (bAo.Insert(mAo) == 1)
                    {
                        sValue = "001";
                    }
                    else
                    {
                        sValue = "011";
                    }
                }
                else
                {
                    sValue = "010";
                }
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty, sValue);
            return sValue;
        }
        private String DeleteAds(Int64 iUserId, Int64 iAdid)
        {
            B_Users bUs = new B_Users();
            String sValue = "998";
            try
            {
                //DataTable dDt = new DataTable();
                //dDt = bUs.SelectUserWeiboId(iWeiboUserId, iWeiboFlag);
                //if (dDt.Rows.Count != 0)
                //{
                //    if (dDt.Rows[0]["Password"].ToString().Trim() == sPassWord)
                //    {
                //        sValue = "001";
                //    }
                //    else
                //    {
                //        sValue = "003";
                //    }
                //}
                //else
                //{
                //    sValue = "002";
                //}
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty, sValue);
            return sValue;
        }
        private String RepeatAds(Int64 iUserId, String sAdid)
        {
            B_AdRepost bAr = new B_AdRepost();
            M_AdRepost mAr = new M_AdRepost();
            String sValue = "998";
            try
            {
                String[] sArrayAdid = sAdid.Split(',');
                for (int i = 0; i < sArrayAdid.Length; i++)
                {
                    mAr.RepostTime = System.DateTime.Now;
                    mAr.RepostUserId = iUserId;
                    mAr.AdId = Convert.ToInt64(sArrayAdid[i]);
                    if (bAr.Insert(mAr) == 1)
                    {
                        sValue = "001";
                    }
                    else
                    {
                        sValue = "011";
                    }
                }
            }
            catch { }
            sValue = JsonParameters.CreateJsonParameters(String.Empty, sValue);
            return sValue;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
